package cn.genmer.test.security.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

/**
 * @program: cuckoo-rms
 * @description: 时间工具类
 * @author: Tan.
 * @create: 2020-07-06 09:19
 **/
public class DateUtil {

    private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_PATTERN = "yyyyMMdd";
    public final static String PATTERN = "yyyyMMdd HHmmss";
    public static Date dateToISODate(String oldDateStr) {
        if (StringUtils.isBlank(oldDateStr)){
            return null;
        }
        Date date = null;
        Date date1 = null;
        String dateStr = null;
        try {
            dateStr = oldDateStr.replace("Z", " UTC");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            date1 = df.parse(dateStr);
            SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date = df1.parse(date1.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * @desc:String转date
     * @param strDate
     * @param format
     * @return 若失败则返回当前时间
     */
    public static Date parseToDate(String strDate, String format) {
        try {
            if (strDate == null || strDate.trim().length() == 0) {
                return new Date();
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(strDate);
        } catch (ParseException e) {
            logger.error("[ERROR：时间转换失败;]", e);
            return new Date();
        }
    }

    /**
     * @desc:格式化日期
     * @author Clyde
     * @date:2014-2-23
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, String format) {
        String customDate = "";
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            customDate = sdf.format(date);
        }
        return customDate;
    }

    /**
     * 时间格式
     * @Author Xw.H
     * @param
     * @return
     **/
    public static LocalDateTime formatTime(String time, String pattern){
        return LocalDateTime.parse(time, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 按默认格式格式化时间
     * @param
     * @return
     **/
    public static Date formatD(String dateStr){
        return formatD(dateStr,DATE_TIME_PATTERN);
    }

    /**
     * 按指定格式格式化时间
     * @Author Xw.H
     * @param
     * @return
     **/
    public static Date formatD(String dateStr ,String format)  {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date ret = null ;
        try {
            ret = simpleDateFormat.parse(dateStr) ;
        } catch (ParseException e) {
            //
        }
        return ret;
    }
    public static void main(String[] args) {
        System.out.println(formatD("20211012",DATE_PATTERN));
    }
}
